package decorationPrice.entity;

import decorationPrice.abs.Fruit;
import decorationPrice.strategy.PromotionStrategy;

import java.util.ArrayList;
import java.util.List;

// 购物车类
public class ShoppingCart {
    private List<Fruit> fruits;
    private List<PromotionStrategy> promotionStrategies;

    public ShoppingCart() {
        this.fruits = new ArrayList<>();
        this.promotionStrategies = new ArrayList<>();
    }

    public void addFruit(Fruit fruit) {
        fruits.add(fruit);
    }

    public void addPromotionStrategy(PromotionStrategy strategy) {
        promotionStrategies.add(strategy);
    }

    public double calculateSubtotal() {
        return fruits.stream().mapToDouble(Fruit::calculateCost).sum();
    }

    public double calculateTotalCost() {
        double subtotal = calculateSubtotal();
        double total = subtotal;
        
        // 应用所有促销策略
        for (PromotionStrategy strategy : promotionStrategies) {
            total = strategy.apply(total, fruits);
        }
        
        return total;
    }

    public void printReceipt() {
        System.out.println("===== 购物清单 =====");
        for (Fruit fruit : fruits) {
            System.out.printf("%s: %.2f元/kg × %.2fkg = %.2f元\n", 
                fruit.getName(), fruit.getPrice(), fruit.getWeight(), fruit.calculateCost());
        }
        System.out.println("===================");
        System.out.printf("小计: %.2f元\n", calculateSubtotal());
        System.out.printf("总计: %.2f元\n", calculateTotalCost());
    }


}